package cn.shyouchi.wuguanjia.android.ui.reportstatistic;


import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.attendance.AttendanceApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.utils.ToastUtil;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class ReportStatisticActivity extends BaseActivity {

    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.rv_report_statistic)
    RecyclerView mRvReportStatistic;


    AttendanceApi mAttendanceApi;
    ReportStatisticAdapter mReportStatisticAdapter;


    List<ReportStatisticEntity.ResultMessageBean> mReportStatisticEntities = new ArrayList<>();


    String projectId;


    @Override
    public int initContentView() {
        return R.layout.activity_report_statistic;
    }

    @Override
    public void initInjector() {
        mAttendanceApi = new AttendanceApi(mContext);
        projectId = getIntent().getStringExtra("projectid");
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());

        mReportStatisticAdapter = new ReportStatisticAdapter(mReportStatisticEntities, projectId);
        mRvReportStatistic.setAdapter(mReportStatisticAdapter);
        mRvReportStatistic.setLayoutManager(new LinearLayoutManager(mContext));


        fetchReportStatistic();
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    private void fetchReportStatistic() {
        mAttendanceApi.getReportStatistic(projectId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ReportStatisticEntity>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        ToastUtil.showToast(e.getMessage());
                    }

                    @Override
                    public void onNext(ReportStatisticEntity entity) {
                        mReportStatisticEntities.clear();
                        mReportStatisticEntities.addAll(entity.getResultMessage());
                        mReportStatisticAdapter.notifyDataSetChanged();
                    }
                });


    }

}
